//
//  LGQRCodeViewController.m
//  RedSunDigitalDoctor
//
//  Created by 1234 on 2022/9/5.
//  二维码

#import "LGQRCodeViewController.h"
#import "CIImage+SGExtension.h"
@interface LGQRCodeViewController ()
/**   */
//@property (nonatomic , strong)LGNavigationView *navigationView;

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIImageView *headPic;
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UILabel *postitleLabel;
/**   */
@property (nonatomic , strong)UILabel *hospitalLabel;
/**   */
@property (nonatomic , strong)UIImageView *codeView;
/**   */
@property (nonatomic , strong)UILabel *descLabel;

@end

@implementation LGQRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"二维码名片";
    [self.view addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.headPic];
    [self.baseView addSubview:self.nameLabel];
    [self.baseView addSubview:self.postitleLabel];
    [self.baseView addSubview:self.hospitalLabel];
    [self.baseView addSubview:self.codeView];
    [self.baseView addSubview:self.descLabel];
    [self.hospitalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.postitleLabel.mas_bottom).offset(viewPix(5));
        make.left.equalTo(self.postitleLabel);
        make.right.equalTo(self.baseView).offset(-viewPix(20));
    }];
}


-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    [self.headPic sd_setImageWithURL:[NSURL URLWithString:dataDic[@"avatar_url"]] placeholderImage:defaultDoctorPic];
    self.nameLabel.text = dataDic[@"realname"];
    self.postitleLabel.text = dataDic[@"profess"];
    self.hospitalLabel.text = dataDic[@"hospital"];
    [self createGenerateQRCode:dataDic[@"qrcode"]];
}

// 生成二维码
- (void)createGenerateQRCode:(NSString *)content{
    
    // 1、创建滤镜对象
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 恢复滤镜的默认属性
    [filter setDefaults];
    
    //2 将字符串转换成
    NSData *infoData = [content dataUsingEncoding:NSUTF8StringEncoding];
    
    // 通过KVC设置滤镜inputMessage数据
    [filter setValue:infoData forKeyPath:@"inputMessage"];
    
    // 3、获得滤镜输出的图像
    CIImage *outputImage = [filter outputImage];

    // 5、将CIImage转换成UIImage，并放大显示
    CGFloat imageViewW = self.codeView.bounds.size.width;
    self.codeView.image = [outputImage SG_createNonInterpolatedWithSize:imageViewW];
}


- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

#pragma mark
#pragma mark ====> 创建控件

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(15), viewPix(30)+iPhoneXMargin(30), Screen_W-viewPix(30), viewPix(509))];
        [_baseView setShadoWithColor:@"#7BC0FF" offset:CGSizeMake(0, 2) opacity:0.1 radius:5];
        _baseView.layer.borderColor = [UIColor colorWithString:@"#5BA8FF"].CGColor;
        _baseView.layer.borderWidth = 1;
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.clipsToBounds = YES;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:AppTitle colorString:@"#FFFFFF" textFont:LGFontWeight(20, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(0, 0, Screen_W-viewPix(30), viewPix(56));
        _titleLabel.backgroundColor = [UIColor colorWithString:@"#5BA8FF"];
    }
    return _titleLabel;
}

-(UIImageView *)headPic{
    if(!_headPic){
        _headPic = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(44), kMaxY(self.titleLabel.frame)+viewPix(46), viewPix(68), viewPix(68))];
        _headPic.backgroundColor = [UIColor colorWithString:@"#EEEEEE"];
        _headPic.cornerRidus = 5;
    }
    return _headPic;
}

-(UILabel *)nameLabel{
    if(!_nameLabel){
        _nameLabel = [UILabel lableWithText:@"昵称" colorString:@"#333333" textFont:LGFont(18) textAlignment:NSTextAlignmentLeft lines:1];
        _nameLabel.frame = CGRectMake(kMaxX(self.headPic.frame)+viewPix(19), kMinY(self.headPic.frame), viewPix(150), viewPix(23));
    }
    return _nameLabel;
}

-(UILabel *)postitleLabel{
    if(!_postitleLabel){
        _postitleLabel = [UILabel lableWithText:@"昵称" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _postitleLabel.frame = CGRectMake(kMinX(self.nameLabel.frame), kMaxY(self.nameLabel.frame)+viewPix(5), viewPix(150), viewPix(23));
    }
    return _postitleLabel;
}

-(UILabel *)hospitalLabel{
    if(!_hospitalLabel){
        _hospitalLabel = [UILabel lableWithText:@"所在医院" colorString:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:2];
        _hospitalLabel.frame = CGRectMake(kMinX(self.nameLabel.frame), kMaxY(self.postitleLabel.frame)+viewPix(5), viewPix(180), viewPix(20));
    }
    return _hospitalLabel;
}

-(UIImageView *)codeView{
    if(!_codeView){
        _codeView = [[UIImageView alloc]initWithFrame:CGRectMake((Screen_W-viewPix(30)-viewPix(256))/2.0, kMaxY(self.headPic.frame)+viewPix(20), viewPix(256), viewPix(256))];
    }
    return _codeView;
}

-(UILabel *)descLabel{
    if(!_descLabel){
        _descLabel = [UILabel lableWithText:@"扫描二维码向我报到" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
        _descLabel.frame = CGRectMake(0, kMaxY(self.codeView.frame)+viewPix(18), Screen_W-viewPix(30), viewPix(20));
    }
    return _descLabel;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
